﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyQQ
{
    partial class LoginForm :CCWin.CCSkinMain
    {
        public LoginForm()
        {
            InitializeComponent();
        }
         
        public LoginForm(string name,string password)
        {
            InitializeComponent();
            this.txtName.Text = name;
            this.txtPass.Text = password;
        }


        private string name;
        private string password;

        public void btnLogin_Click(object sender, EventArgs e)
        {
            name = this.txtName.Text.Trim();
            password = this.txtPass.Text.Trim();

            Message.Message loginMsg = new Message.Message();
            if (name != string.Empty && password != string.Empty)
            {
                loginMsg.Type = Message.MessageType.Login;
                loginMsg.Login = new Message.LoginMessage(name, password);
                loginMsg.IP = new Message.IpMessage(Net.NetReciver.ip, Net.NetReciver.port);
                Net.NetSender.Sender.SendMessage(loginMsg);
                Net.NetReciver.Listener.MessageArrived += ReciveMsg;
            }
            else
            {
                return;
            }
        }

        public void ReciveMsg(Message.Message msg)
        {
            if (msg.Type == Message.MessageType.Login)//是登陆信息的返回窗口
            {
                if (msg.Login.success == true)
                {
                    this.Visible = false;
                    MainForm mf= new MainForm(name);
                    mf.Show();
                    Net.NetReciver.Listener.MessageArrived -= ReciveMsg;
                    mf.GetInfo(msg);
                    Net.NetReciver.Listener.MessageArrived += mf.GetInfo;
                }
            }

        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            new RegisterForm().Show();

            this.Visible = false;
            //this.Close();
        }
    }
}
